/**
 * Created by proit on 10/28/2014.
 */
$(function() {
    DtShop.uploadFile($('#uploadFile'));
    DtShop.updateStatus();
});

var DtShop = {
    uploadFile: function (fileSelector) {
        fileSelector.fileupload(
            {
                dataType: 'json',
                type: 'POST',
                url: fileSelector.attr('url'),
                options: {singleFileUploads: true},

                submit: function () {
                    $('.container .dt-style-container').addClass('loading');
                },

                send: function(e, data) {
                    $('#image-show').remove();
                },

                done: function(e, data) {
                    $('.container .dt-style-container').removeClass('loading');

                    if (typeof data.result.files[0].error !== 'undefined') {
                        alert(data.result.files[0].error);

                        return;
                    }

                    if ($.type(data.result.files[0])!== 'undefined') {
                        $('#uploadFile').after($('<img>').attr({'id': 'image-show', 'src': public_path + '/' + data.result.files[0].thumbnailUrl}).css({'padding-left': '70px'}));
                        $('input:hidden[name="product_image_thumbnail"]').val(data.result.files[0].thumbnailUrl);
                        $('input:hidden[name="product_image_main"]').val(data.result.files[0].url);
                    } else {
                        return false;
                    }
                },

                fail: function(e, data) {
                    alert('File uploaded does not success!!!');
                    $('.container .dt-style-container').removeClass('loading');
                }
            }
        );
    },
    /**
     *
     */
    updateStatus: function () {
        var parent = this;

        $('.status').on('click', function () {
            var id = $(this).data('id'), model = $(this).data('model')
                var onclick = $(this),
                options = {
                url: root_path + '/dtu/admin/' + model + '/updateStatus',
                type: 'get',
                dataType: 'json',
                data: {id: id},
                beforeSend: function () {
                    onclick.attr({'src': public_path + '/images/ajax-loader.gif'}).fadeIn(400);
                },
                complete: function (result) {
                    if (result.responseJSON.error == false) {
                        setTimeout(function () {
                            if (result.responseJSON.status == 1) {
                                onclick.attr('src', public_path + '/images/active.png');
                            } else {
                                onclick.attr('src', public_path + '/images/inactive.png');
                            }
                        }, 400);
                    }
                },
                error: function (error) {

                }
            };

            parent.UpdateStatusAjax(options);
        })
    },
    /**
     *
     * @param options
     * @returns {boolean}
     * @constructor
     */
    UpdateStatusAjax: function (options) {
        if (options.length == 0) {
          return false;
        }

        $.ajax({
            url: options['url'],
            type: options['type'],
            dataType: options['dataType'],
            beforeSend: options['beforeSend'],
            data: options['data'],
            complete: options['complete'],
            error: options['error']
        })

        return true;
    }
}